﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/// <summary>
/// 一般数据结构
/// 
/// </summary>
namespace _05_data_struct
{
    class Program
    {
        static void Main(string[] args)
        {
            //Nullable();
            Struct();
        }

        /// <summary>
        /// 结构体
        /// https://www.runoob.com/csharp/csharp-struct.html
        /// </summary>
        static void Struct()
        {
            Books Book1 = new Books(); /* 声明 Book1，类型为 Books */
            Books Book2 = new Books(); /* 声明 Book2，类型为 Books */

            /* book 1 详述 */
            Book1.setValues("C Programming",
            "Nuha Ali", "C Programming Tutorial", 6495407);

            /* book 2 详述 */
            Book2.setValues("Telecom Billing",
            "Zara Ali", "Telecom Billing Tutorial", 6495700);

            /* 打印 Book1 信息 */
            Book1.display();

            /* 打印 Book2 信息 */
            Book2.display();

            Console.ReadKey();
        }

        /// <summary>
        /// 结构体字段不允许赋初始值
        /// </summary>
        struct Books
        {
            private string title;
            private string author;
            private string subject;
            private int book_id;
            public void setValues(string t, string a, string s, int id)
            {
                title = t;
                author = a;
                subject = s;
                book_id = id;
            }
            public void display()
            {
                Console.WriteLine("Title : {0}", title);
                Console.WriteLine("Author : {0}", author);
                Console.WriteLine("Subject : {0}", subject);
                Console.WriteLine("Book_id :{0}", book_id);
            }

        };

        /// <summary>
        /// 可空类型
        /// https://www.runoob.com/csharp/csharp-nullable.html
        /// ? 单问号用于对 int、double、bool 等无法直接赋值为 null 的数据类型进行 null 的赋值，意思是这个数据类型是 Nullable 类型的。
        /// ?? 可以理解为三元运算符的简化形式：
        /// num3 = num1 ?? 5.34;
        /// num3 = (num1 == null) ? 5.34 : num1;
        /// </summary>
        static void Nullable()
        {
            //int? i = 3;
            // 等同于
            //Nullable<int> i = new Nullable<int>(3);

            //int i; //默认值0
            //int? ii; //默认值null


            int? num1 = null;
            int? num2 = 45;
            double? num3 = new double?();
            double? num4 = 3.14157;

            bool? boolval = new bool?();

            // 显示值
            //显示可空类型的值： , 45,  , 3.14157
            Console.WriteLine("显示可空类型的值： {0}, {1}, {2}, {3}",
                               num1, num2, num3, num4);
            //一个可空的布尔值：
            Console.WriteLine("一个可空的布尔值： {0}", boolval);


            double? n1 = null;
            double? n2 = 3.14157;
            double n3;
            n3 = n1 ?? 5.34;      // num1 如果为空值则返回 5.34
            Console.WriteLine("n3 的值： {0}", n3); //n3 的值： 5.34
            n3 = n2 ?? 5.34;
            Console.WriteLine("n3 的值： {0}", n3); // n3 的值： 3.14157


            Console.ReadKey();
            
        }

    }
}
